const router = require('koa-router')();
const qiniu = require('qiniu');
const config = require('../config/config');

const adminController = require('../controllers/Admin');

router.prefix('/admin'); //加默认前缀

/* 添加和编辑产品路由  /post-add-edit-product-data */
router.post('/post-add-edit-product-data', adminController.postProductData)
/* 删除产品 /post-delete-product*/
router.post('/post-delete-product', adminController.postDeleteProduct);

// /admin/add-product
router.get('/add-product', adminController.getAddProduct);

// /admin/edit-product
router.get('/edit-product/:pid', adminController.getEditProduct);

// /admin/manage-product
router.get('/manage-product', adminController.getManageProduct);

// /admin/qiniu-token
router.get('/get-qiniu-token', (ctx, next) => {
  var accessKey = config.qiniu.keys.ak;
  var secretKey = config.qiniu.keys.sk;
  var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

  //自定义凭证有效期（示例2小时，expires单位为秒，为上传凭证的有效时间）
  var options = {
    scope: config.qiniu.bucket.xingsheng.bucket_name,
    expires: 7200
  };
  var putPolicy = new qiniu.rs.PutPolicy(options);
  var uploadToken = putPolicy.uploadToken(mac);
  let resJson = JSON.stringify({
    upToken: uploadToken
  });
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.body = {
    code:200,
    uploadToken: uploadToken,
}
  //发送响应 ctx.body
})

module.exports = router;